package com.red.controller.admin;

import com.red.dto.AdminsLoginDTO;
import com.red.dto.AdminsPageQueryDTO;
import com.red.entity.Admins;
import com.red.result.PageResult;
import com.red.result.Result;
import com.red.service.AdminsService;
import com.red.vo.AdminsVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("admin/admin")
@ApiModel(value = "管理端的管理员接口")
@Slf4j
@CrossOrigin
public class AdminsController {
    @Autowired
    private AdminsService adminsService;


    /**
     * 管理员登录
     * @param dto
     * @return
     */
    @PostMapping("/login")
    @CrossOrigin
    @ApiModelProperty("管理员登录")
    public Result<AdminsVO> login(@RequestBody AdminsLoginDTO dto){
        AdminsVO adminsVO = adminsService.login(dto);
        return Result.success(adminsVO);
    }

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @PostMapping("/users")
    @CrossOrigin
    public Result<PageResult> pageQuery(@RequestBody AdminsPageQueryDTO dto){
        log.info("管理员分页查询:{}",dto);
        PageResult pageResult = adminsService.pageQuery(dto);
        return Result.success(pageResult);
    }

    /**
     * 查询信息
     * @param
     * @return
     */
    @GetMapping()
    @CrossOrigin
    public Result<Admins> adminInfo(){
        Admins admins = adminsService.getAdminInfo();
        return Result.success(admins);
    }

    /**
     * 修改信息
     * @return
     */
    @PostMapping("/update")
    @CrossOrigin
    public Result update(@RequestBody Admins admins){
        adminsService.updateAdmins(admins);
        return Result.success();
    }

    /**
     * 删除管理员
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @CrossOrigin
    public Result delete(@PathVariable Long id){
        adminsService.deleteById(id);
        return Result.success();
    }

    /**
     * 新增管理员
     * @param admins
     * @return
     */
    @PostMapping("/insert")
    @CrossOrigin
    public Result insert(@RequestBody Admins admins){
        adminsService.insert(admins);
        return Result.success();
    }
}
